/**
  ******************************************************************************
  * @file    pid_driver.h
  * @author  Sherlock
  * @version V0.1
  * @date    8/12/2024
  * @brief   PID driver.
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef  __PID_DRIVER_H__
#define  __PID_DRIVER_H__

#ifdef __cplusplus
 extern "C" {
#endif

/**
  * @brief GLOBALS RAM definition
  */
#ifdef   PID_DRIVER_GLOBAL
#define  PID_DRIVER_EXT
#else
#define  PID_DRIVER_EXT  extern
#endif


/**
  * @brief  INCLUDE FILES.
  */



/***********************************************************
 *                   PID Timer define   
***********************************************************/
/* TIM1 */
#define TIM1CountCLK_FREQ                  ((uint32_t) 4800000)   /* 4.8MHZ */
                                           
#define PIDTIM_RCCPeriph                   RCC_APB2_PERIPH_TIM1
#define PIDTIM_Prescaler                   (uint16_t)(SYSMAXCLK_FREQ/TIM1CountCLK_FREQ-1)
#define PIDTIM_CounterMode                 TIM_CNT_MODE_UP
#define PIDTIM_Period                      47999                           //100HZ
#define PIDTIM_ClockDivision               TIM_CLK_DIV1
#define PIDTIM_RepetitionCounter           1
#define PIDTIM                             TIM1

#define PIDTIM_OCMode                      TIM_OCMODE_PWM2    
#define PIDTIM_OutputState                 TIM_OUTPUT_STATE_ENABLE
#define PIDTIM_OutputNState                TIM_OUTPUT_NSTATE_DISABLE
#define PIDTIM_Pulse                       47999                    //0ms pulse
#define PIDTIM_OCPolarity                  TIM_OC_POLARITY_HIGH
#define PIDTIM_OCNPolarity                 TIM_OCN_POLARITY_LOW 
#define PIDTIM_OCIdleState                 TIM_OC_IDLE_STATE_RESET 
#define PIDTIM_OCNIdleState                TIM_OCN_IDLE_STATE_RESET 

/* TIM4，临时用于锁相环 */
#define TIM4CountCLK_FREQ                  ((uint32_t) 36000000)   /* 36MHZ */
                                           
#define PIDTIM4_RCCPeriph                   RCC_APB1_PERIPH_TIM4
#define PIDTIM4_Prescaler                   (uint16_t)(SYSMAXCLK_FREQ/TIM4CountCLK_FREQ-1)
#define PIDTIM4_CounterMode                 TIM_CNT_MODE_UP
#define PIDTIM4_Period                      7199                           //5kHZ
#define PIDTIM4_ClockDivision               TIM_CLK_DIV1
#define PIDTIM4_RepetitionCounter           1
#define PIDTIM4                             TIM4

#define PIDTIM4_OCMode                      TIM_OCMODE_PWM1
#define PIDTIM4_OutputState                 TIM_OUTPUT_STATE_ENABLE
#define PIDTIM4_OutputNState                TIM_OUTPUT_NSTATE_DISABLE
#define PIDTIM4_Pulse                       7199                      //0ms pulse
#define PIDTIM4_OCPolarity                  TIM_OC_POLARITY_HIGH
#define PIDTIM4_OCNPolarity                 TIM_OCN_POLARITY_LOW 
#define PIDTIM4_OCIdleState                 TIM_OC_IDLE_STATE_RESET 
#define PIDTIM4_OCNIdleState                TIM_OCN_IDLE_STATE_RESET 

/***********************************************************
*                    PID GPIO define   
***********************************************************/
#define PIDGPIO_OutputRCCPeriph           RCC_APB2_PERIPH_GPIOA
#define PIDGPIO_OutputMode                GPIO_Mode_AF_PP
#define PIDGPIO_OutputSpeed               GPIO_Speed_50MHz
#define PIDGPIO_OutputPort                GPIOA
#define PIDGPIO_OutputBitx                GPIO_PIN_8
#define PIDGPIO_OutputPinRemap            GPIO_PART2_RMP_TIM1

#define PIDGPIO_SyncSwitchRCCPeriph       RCC_APB2_PERIPH_GPIOA
#define PIDGPIO_SyncSwitchMode            GPIO_Mode_Out_PP
#define PIDGPIO_SyncSwitchSpeed           GPIO_Speed_50MHz
#define PIDGPIO_SyncSwitchPort            GPIOA
#define PIDGPIO_SyncSwitchBitx            GPIO_PIN_15

#define PIDGPIO_PWM2RCCPeriph             RCC_APB2_PERIPH_GPIOB
#define PIDGPIO_PWM2Mode                  GPIO_Mode_AF_PP
#define PIDGPIO_PWM2Speed                 GPIO_Speed_50MHz
#define PIDGPIO_PWM2Port                  GPIOB
#define PIDGPIO_PWM2Bitx                  GPIO_PIN_9

#define PIDGPIO_SyncStatusRCCPeriph       RCC_APB2_PERIPH_GPIOB
#define PIDGPIO_SyncStatusMode            GPIO_Mode_IN_FLOATING
#define PIDGPIO_SyncStatusSpeed           GPIO_Speed_50MHz
#define PIDGPIO_SyncStatusPort            GPIOB
#define PIDGPIO_SyncStatusBitx            GPIO_PIN_14

#define PIDGPIO_StatorOut1RCCPeriph       RCC_APB2_PERIPH_GPIOD
#define PIDGPIO_StatorOut1Mode            GPIO_Mode_Out_PP
#define PIDGPIO_StatorOut1Speed           GPIO_Speed_50MHz
#define PIDGPIO_StatorOut1Port            GPIOD
#define PIDGPIO_StatorOut1Bitx            GPIO_PIN_0

#define PIDGPIO_StatorOut2RCCPeriph       RCC_APB2_PERIPH_GPIOC
#define PIDGPIO_StatorOut2Mode            GPIO_Mode_Out_PP
#define PIDGPIO_StatorOut2Speed           GPIO_Speed_50MHz
#define PIDGPIO_StatorOut2Port            GPIOC
#define PIDGPIO_StatorOut2Bitx            GPIO_PIN_12


/**
  * @brief Functions
  */
PID_DRIVER_EXT uint8_t PID_HWInit(void);
PID_DRIVER_EXT uint8_t PID_HWEnable(void);


#ifdef __cplusplus
}
#endif

#endif   /*  __PID_DRIVER_H__  */

/******************* (C) COPYRIGHT 2010 Feirui *****END OF FILE****/